import { StorageSerializers, useStorage, useToggle } from '@vueuse/core';
type LoginInfo = any;
export const useLoginStore = defineStore('login', function () {
  /** 登录信息 */
  const loginInfo = ref<LoginInfo | null>(null);
  useStorage('ykst-login-info', loginInfo, undefined, {
    serializer: StorageSerializers.object,
  });

  /** 登录加载状态 */
  const [loginLoading, toggleLoginLoading] = useToggle(false);

  /** 注册状态 */
  const [regLoading, toggleRegLoading] = useToggle(false);

  /** 刷新用户状态 */
  const [refreshLoading, toggleRefreshLoading] = useToggle(false);

  /** 保存登录信息 */
  const saveLoginInfo = (data: LoginInfo) => (loginInfo.value = data);

  /** 清空登录信息 */
  const clear = () => (loginInfo.value = null);

  /**
   * 登录
   */
  const login = () => {};
  // async (...arg: Parameters<typeof loginApi>) => {
  //   try {
  //     toggleLoginLoading(true);
  //     const res = await loginApi(...arg);
  //     saveLoginInfo(res.data);
  //     await routerPush({ name: 'Home' });
  //   } catch (error) {
  //     return Promise.reject(error);
  //   } finally {
  //     toggleLoginLoading(false);
  //   }
  // };

  /**
   * 退出登录
   */
  function logout() {
    clear();
    routerPush({ name: 'Auth' });
  }
  /** 刷新用户信息
   * @param [toHomePage=false]  是否导航至home页面
   */
  async function refreshUserInfo(toHomePage: boolean = false) {
    try {
      toggleRefreshLoading(true);
      // const res = await meApi();
      // saveLoginInfo(res.data);
      // toHomePage === true && (await routerPush({ name: 'Home' }));
    } catch (error) {
      return Promise.reject(error);
    } finally {
      toggleRefreshLoading(false);
    }
  }

  /** 刷新用户信息，并且定导航至home页面 */

  return {
    loginInfo,
    loginLoading,
    regLoading,
    refreshLoading,
    login,
    logout,
    clear,
    refreshUserInfo,
  };
});

function logoutRequest() {
  throw new Error('Function not implemented.');
}
